// pages/reward/reward.js
const utils = require('../../utils/util.js');
const post = utils.post;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: '',
    flag: 'none',
    errmsg: '',
    name: 'APP',
    ntype: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      let _scene = decodeURIComponent(options.scene);
      this.data.uid = _scene;
      this.setData({
        name: '公众号',
        ntype: 2
      });
    } else {
      if (options.uid) {
        this.data.uid = options.uid;

        wx.setStorageSync('weapp_uid', this.data.uid);

        //console.log(this.data.uid);
        this.setData({
          name: 'APP',
          ntype: 1
        });

        if (options.from && 'xlist' == options.from) {
          this.setData({
            name: '',
            ntype: 0
          });
        }
      }
    }
  },

  launchAppError: function (e) {
    //console.log(e.detail.errMsg)
    this.setData({
      errmsg: e.detail.errMsg
    });

    wx.showToast({
      title: e.detail.errMsg,
      icon: 'none',
    });
  },

  handleReturn() {
    this.setData({
      flag: 'none'
    });

    wx.navigateBack({
      delta: 1
    });
  },

  handleReward() {
    wx.showLoading({
      title: '正在登录中',
    })

    let that = this;
    wx.login({
      success(res) {
        var code = res.code;
        wx.getUserInfo({
          success(res2) {
            var encryptedData = res2.encryptedData;
            var iv = res2.iv;
            var p = {
              m: 'jump_reward_bind',
              uid: that.data.uid,
              code: code,
              encryptedData: encryptedData,
              iv: iv
            };
            post(p).then(res3 => {
              //console.log(res3);
              wx.hideLoading();

              if (0 == res3.r) {
                // 任务完成，点击返回APP
                that.setData({
                  flag: 'flex'
                });

                var userinfo = res3.userinfo;
                var weapp_openid = userinfo.openId;
                wx.setStorageSync('weapp_openid', weapp_openid); // 桔子派的openId

                wx.setStorageSync('_userinfo', res3.userinfo);
                wx.setStorageSync('device_info', wx.getSystemInfoSync());
              } else {
                wx.showToast({
                  title: res3.errmsg,
                  icon: 'none',
                });
              }
            });
          }
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  /*onShareAppMessage: function () {
  
  }*/

  submitFormId(e) {
    //console.log(e);
    let formId = e.detail.formId;
    let that = this;
    let button = e.detail.target.dataset.name;
    let page = getCurrentPageUrlWithArgs();
    let p = {
      m: 'colform',
      uid: that.data.uid,
      openid: wx.getStorageSync('weapp_openid'),
      formId: formId
    };
    if (page && page.length > 0) {
      p.page = page;
    }
    if (button && button.length > 0) {
      p.button = button;
    }
    //console.log(p)
    post(p).then(res => {
    })
  }
})